package com.loong.member.handler;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.loong.common.enumerate.ResultCode;
import com.loong.common.exception.GlobalException;
import com.loong.common.rest.rpc.RPCResponse;
import com.loong.common.utils.TransformUtils;
import com.loong.data.dto.GoodsDTOResult;
import com.loong.data.dto.GoodsSkuDTOResult;
import com.loong.data.dto.GoodsSpecificationDTOResult;
import com.loong.data.service.IGoodsService;
import com.loong.data.service.IGoodsSpecificationService;
import com.loong.member.business.CartItemBusiness;
import com.loong.member.common.forehead.result.CartItemForeheadResult;
import com.loong.member.model.CartItem;
import com.loong.shop.dto.GoodsSkuDTOResultInShop;
import com.loong.shop.service.IGoodsSkuService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.List;
import java.util.Objects;

/**
 * @Description: 处理购物详情业务的handler
 * @Author: 刘群
 * @Date: 2020/10/23 17:22
 */
@Slf4j
@Component
public class CartItemHandler {

    @Resource
    private CartItemBusiness itemBusiness;

    @DubboReference
    private IGoodsService goodsService;

    @DubboReference
    private IGoodsSpecificationService specificationService;

    @DubboReference
    private IGoodsSkuService goodsSkuService;

    /**
     * 新增购物详情
     *
     * @param cartItem 购物详情
     */
    protected void saveCartItem(CartItem cartItem) {
        itemBusiness.save(cartItem);
    }

    /**
     * 根据购物车id删除购物详情
     *
     * @param cartId 购物车id
     */
    protected void deleteCartItemByCartId(Long cartId) {
        itemBusiness.deleteCartItemByCartId(cartId);
    }

    /**
     * 根据购物车id查询购物详情
     *
     * @param cartId 购物车id
     * @return 购物详情集合
     */
    protected List<CartItemForeheadResult> findCartItemByCartId(Long cartId) {
        List<CartItem> cartItemList = itemBusiness.findCartItemByCartId(cartId);
        if (CollectionUtils.isEmpty(cartItemList)) {
            return null;
        } else {
            List<CartItemForeheadResult> itemResultList = TransformUtils.transformList(cartItemList, CartItemForeheadResult.class);
            for (CartItemForeheadResult cartItemForeheadResult : itemResultList) {
                //根据goodsSku的id查询Goods。
                RPCResponse<GoodsDTOResult> rpcResponse = goodsService.findGoodsBySkuId(cartItemForeheadResult.getGoodsSkuId());
                GoodsDTOResult goodsDTO = rpcResponse.getData();
                if (ObjectUtils.isEmpty(goodsDTO)) throw new GlobalException(ResultCode.DATA_ERROR, "没有查询到商品");
                //添加商品GoodsBase的id到购物车详情结果中
                cartItemForeheadResult.setGoodsBaseId(goodsDTO.getId());
                //添加商品分类id到购物车详情结果中
                cartItemForeheadResult.setGoodsCategoryId(goodsDTO.getCategoryId());
                //添加商品名称到购物详情结果中
                cartItemForeheadResult.setGoodsName(goodsDTO.getName());
                //添加商品包装费到购物详情结果中
                cartItemForeheadResult.setPackingExpense(goodsDTO.getPackingExpense());
                //根据商品skuId查询商品sku
                RPCResponse<GoodsSkuDTOResultInShop> rpcResponseGoodsSku = goodsSkuService.findGoodsSkuBySkuId(cartItemForeheadResult.getGoodsSkuId());
                GoodsSkuDTOResultInShop goodSkuDTO = rpcResponseGoodsSku.getData();
                if (ObjectUtils.isEmpty(goodSkuDTO)) throw new GlobalException(ResultCode.DATA_ERROR, "没有查询到商品sku");
                //添加商品图片到购物详情结果中
                cartItemForeheadResult.setGoodsPicture(goodSkuDTO.getSpecificationPics());
                //添加商品原价到购物详情结果中
                cartItemForeheadResult.setGoodsOriginPrice(goodSkuDTO.getOriginPrice());
                //添加商品现价到购物详情结果中
                cartItemForeheadResult.setGoodsCurrentPrice(goodSkuDTO.getCurrentPrice());
                //获取规格组中的规格值id数组
                String specification = goodSkuDTO.getGoodsSpecification();
                String[] idList = StringUtils.substringBetween(specification, "[", "]").split(",");
                //根据规格值id查询查询规格值名称
                StringBuffer specificationValues = new StringBuffer();
                for (String id : idList) {
                    //根据商品规格值id查询规格值
                    RPCResponse<GoodsSpecificationDTOResult> rpcResponse2 = specificationService.findGoodsSpecificationById(Long.valueOf(id));
                    GoodsSpecificationDTOResult specificationDTO = rpcResponse2.getData();
                    if (ObjectUtils.isEmpty(specificationDTO))
                        throw new GlobalException(ResultCode.DATA_ERROR, "没有查询到商品规格");
                    String value = specificationDTO.getGoodsSpecificationValue();
                    specificationValues.append(value).append(",");
                }
                //添加规格组描述到购物详情结果中
                cartItemForeheadResult.setSpecificationDescription(StringUtils.substringBeforeLast(specificationValues.toString(), ","));
            }
            return itemResultList;
        }
    }


}
